package com.hfut.manage.controller;


import com.hfut.manage.pojo.Result;
import com.hfut.manage.pojo.Salary;
import com.hfut.manage.pojo.SubCourse;
import com.hfut.manage.service.impl.SubCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/SubCourse")
public class SubCourseController {
    @Autowired
    private SubCourseService subCourseService;


    @GetMapping
    public Result list() {
        return Result.success(subCourseService.lambdaQuery().orderByAsc(SubCourse::getCourseId).list());
    }

    @PutMapping
    public Result updata(@RequestBody SubCourse subcourse) {
        subCourseService.updateById(subcourse);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody SubCourse salary) {
        subCourseService.save(salary);
        return Result.success();
    }

    @GetMapping("/{name}")
    public Result getById(@PathVariable("name") String name) {
        List<SubCourse> subCourses = subCourseService.lambdaQuery()
                .eq(SubCourse::getCourseName, name)
                .list();
        return Result.success(subCourses);
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Long id) {
        boolean success = subCourseService.removeById(id);
        if (!success) {
            return Result.error(500, "删除失败");
        }
        return Result.success();
    }

}
